﻿using System;
using System.Collections.Generic;
using System.IO;

/// <summary>
/// Log 的摘要说明
/// </summary>
class Log
{
    //日志开关
    private static bool permit = true;
    public static bool Permit
    {
        get { return Log.permit; }
        set { Log.permit = value; }
    }

    /// <summary>
    /// 将异常信息写入日志
    /// </summary>
    /// <param name="strExMsg">异常信息</param>
    public static void writeExceptionLog(string strExMsg)
    {
        try
        {
            //固定HIS日志文件夹
            string logPath = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\ExceptionLog";


            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

            string logName = logPath + "\\" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + "_Log.txt";    //日志文件名

            //定义文件信息对象
            FileInfo finfo = new FileInfo(logName);

            //创建只写文件流
            using (FileStream fs = finfo.OpenWrite())
            {
                //根据文件流创建写数据流
                StreamWriter w = new StreamWriter(fs);
                //设置写数据流的起始位置为文件流的末尾
                w.BaseStream.Seek(0, SeekOrigin.End);
                //写入内容
                w.WriteLine("{0} {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
                w.WriteLine("{0}", strExMsg);
                w.WriteLine("---------------------------------");
                //清空缓冲区内容，并把缓冲区内容写入基础流
                w.Flush();
                //关闭写数据流
                w.Close();
            }
        }
        catch
        {
        }
    }

    /// <summary>
    /// 向指定的位置写日志文件;
    /// pathOfLog,日志文件路径，不包括文件名,默认应用程序目录;
    /// source 错误来源/错误位置信息；用于定位错误,默认为空字符串;
    /// msg 错误信息(分号视作换行)      
    /// </summary>
    public static void write(string pathOfLog, string source, String msg)
    {

        try
        {
            if (Permit.Equals(true))
            {
                if (string.IsNullOrEmpty(pathOfLog))
                {
                    pathOfLog = AppDomain.CurrentDomain.BaseDirectory + "log";
                }

                if (string.IsNullOrEmpty(source))
                {
                    source = "";
                }
                if (string.IsNullOrEmpty(msg))
                {
                    msg = "";
                }

                DateTime dt = System.DateTime.Now;

                string logName = "";
                // logName = dt.Year.ToString() + "_" + dt.Month.ToString() + "_" + dt.Day.ToString()+dt.Hour.ToString()+dt.Minute.ToString() ;
                string date = "";
                //date = dt.Year.ToString() + "_" + dt.Month.ToString() + "_" + dt.Day.ToString() + dt.Hour.ToString(); 
                date = dt.ToString("yyyyMMdd");
                string currentTime = dt.ToString("yyyy-MM-dd HH" + ":" + "mm");

                logName = date + ".log";
                string path = pathOfLog + @"\" + logName;
                //string path = AppDomain.CurrentDomain.BaseDirectory + "\\log\\" + logName + ".txt";
                if (!Directory.Exists(pathOfLog))
                {
                    Directory.CreateDirectory(pathOfLog);
                }

                string[] msgArr = msg.Split(';');
                msg = "";

                if (File.Exists(path))
                {
                    using (StreamWriter sw = File.AppendText(path))
                    {
                        for (int i = 0; i < msgArr.Length; i++)
                        {
                            msg = msg + msgArr[i] + " ;" + sw.NewLine;
                        }
                        //
                        sw.WriteLine(currentTime + "  [" + source + "]");
                        sw.Write(msg + sw.NewLine);
                        sw.WriteLine("----------------------------------------------------------------------" + sw.NewLine);
                        sw.Flush();
                        sw.Close();
                    }
                }
                else
                {
                    // Create a file to write to.
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        for (int i = 0; i < msgArr.Length; i++)
                        {
                            msg = msg + msgArr[i] + " ;" + sw.NewLine;
                        }
                        //
                        sw.WriteLine(currentTime + "  [" + source + "]");
                        sw.Write(msg + sw.NewLine);
                        sw.WriteLine("----------------------------------------------------------------------" + sw.NewLine);
                        sw.Flush();
                        sw.Close();
                    }
                }

            }
        }
        catch
        {

        }
    }

    /// <summary>
    /// 将日志写入固定位置
    /// 错误调试日志函数，source 错误来源，msg 错误信息
    /// </summary>
    public static void write(string source, String msg)
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "log";
        write(path, source, msg);
    }

    /// <summary>
    /// 将日志信息写入
    /// 错误调试日志函数，msg 错误信息
    /// </summary>
    public static void write(String msg)
    {
        try
        {
            if (Permit.Equals(true))
            {
                DateTime dt = System.DateTime.Now;

                string logName = "";
                logName = dt.Year + "_" + dt.Month + "_" + dt.Day + ".txt";
                string currentTime = "";
                currentTime = logName + "  " + dt.Hour + ":" + dt.Minute + ":" + dt.Second;
                string logDir = AppDomain.CurrentDomain.BaseDirectory + "log";
                string path = logDir + @"\" + logName;

                if (!Directory.Exists(logDir))
                {
                    Directory.CreateDirectory(logDir);
                }

                if (File.Exists(path))
                {
                    using (StreamWriter sw = File.AppendText(path))
                    {
                        //
                        sw.WriteLine(currentTime);
                        sw.Write(msg + sw.NewLine);
                        sw.WriteLine("------------------------------------------" + sw.NewLine);
                        sw.Flush();
                        sw.Close();
                    }
                }
                else
                {
                    // Create a file to write to.
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        //
                        sw.WriteLine(currentTime);
                        sw.Write(msg + sw.NewLine);
                        sw.WriteLine("------------------------------------------" + sw.NewLine);
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
        }
        catch
        {
        }
    }

    /// <summary>
    /// 登陆日志函数，记录所有登陆网站操作
    /// </summary>
    /// <param name="location">登录位置</param>
    /// <param name="ip">登录者IP</param>
    /// <param name="userName">登录这是用的用户名</param>
    /// <param name="result">是否登陆成功</param>
    public static void loginRecords(string location, string ip, string userName, string result)
    {
        try
        {
            if (Permit.Equals(true))
            {
                if (string.IsNullOrEmpty(location))
                {
                    location = "";
                }
                if (string.IsNullOrEmpty(ip))
                {
                    ip = "";
                }
                if (string.IsNullOrEmpty(userName))
                {
                    userName = "";
                }
                if (string.IsNullOrEmpty(result))
                {
                    result = "";
                }

                string msg = "IP：" + ip + " \t\t用户名：" + userName + " \t\t登陆：" + location + "  \t\t结果：" + result;
                DateTime dt = System.DateTime.Now;

                string logName = "";
                logName = "login_" + dt.Year + "_" + dt.Month + "_" + dt.Day + ".txt";
                string currentTime = "";
                currentTime = logName + "  " + dt.Hour + ":" + dt.Minute + ":" + dt.Second;
                string logDir = AppDomain.CurrentDomain.BaseDirectory + "log";
                string path = logDir + "log\\login\\" + logName;
                if (!Directory.Exists(logDir))
                {
                    Directory.CreateDirectory(logDir);
                    Directory.CreateDirectory(logDir + @"\login");
                }
                else
                {
                    if (!Directory.Exists(logDir + @"\login"))
                    {
                        Directory.CreateDirectory(logDir + @"\login");
                    }
                }

                if (File.Exists(path))
                {
                    using (StreamWriter sw = File.AppendText(path))
                    {
                        sw.WriteLine(currentTime);
                        //sw.WriteLine("[" + location + "]");
                        sw.Write(msg + sw.NewLine);
                        sw.WriteLine("" + sw.NewLine);
                        sw.Flush();
                        sw.Close();
                    }
                }
                else
                {
                    // Create a file to write.
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        //
                        sw.WriteLine(currentTime);
                        //sw.WriteLine("[" + location + "]");
                        sw.Write(msg + sw.NewLine);
                        sw.WriteLine("" + sw.NewLine);
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
        }
        catch
        {
        }
    }
}